package com.example.demo.module.file.controller;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/file1")
public class FileDemo1Controller {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping
    public String hello(){
        return "file1";
    }


    /**
     * RestTemplate转发文件
     * @param file
     * @return
     */
    @PostMapping("/forwardFile")
    public String forwardFile(@RequestParam("file") MultipartFile file){
        System.out.println("...FileDemo1Controller.forwardFile()... 文件名:"+file.getOriginalFilename());
        String url = "http://127.0.0.1:8080/easy/upload/import1";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
        parts.add("file",file.getResource());
        return restTemplate.postForObject(url, parts, String.class);
    }


    /**
     * httpClient转发文件
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/forwardFile2")
    public String forwardFile2(@RequestParam("file") MultipartFile file) throws Exception{
        System.out.println("...FileDemo1Controller.forwardFile2()... 文件名:"+file.getOriginalFilename());
        String url = "http://127.0.0.1:8080/easy/upload/import1";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        //httpPost.addHeader("Content-Type","multipart/form-data");
        // 创建待处理的文件
        String fileName = file.getOriginalFilename();
        ContentBody files = new ByteArrayBody(file.getBytes(), fileName);
        // 对请求的表单域进行填充
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        //"file"是另一个接口的参数名
        builder.addBinaryBody("file",file.getInputStream(), ContentType.MULTIPART_FORM_DATA,file.getOriginalFilename());
        httpPost.setEntity(builder.build());
        HttpResponse response = httpClient.execute(httpPost);
        String s1 = EntityUtils.toString(response.getEntity());
        return s1;
    }

}
